package com.bilibili.java.bookpage.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.bilibili.java.bookpage.bean.Commodity;
import com.bilibili.java.bookpage.bean.Indent;
import com.bilibili.java.bookpage.bean.Orders;
import com.bilibili.java.bookpage.dao.HomePageDao;
import com.bilibili.java.bookpage.dao.impl.HomePageDaoImpl;
import com.bilibili.java.bookpage.service.HomePageService;
import com.bilibili.java.books.util.Footet;
import com.bilibili.java.books.util.JDBCUtil;

/**
 * 首页业务层
 * @author Administrator
 *
 */
public class HomePageServiceImpl implements  HomePageService{

	HomePageDao dao=new HomePageDaoImpl();
	
	/**
	 * 订单编号查询订单详情
	 * @param orderId
	 * @return
	 */
	public List<Orders> queryOrder(int orderId){
		
		return dao.queryOrder(orderId);
		
	}
	
	//取出数据库中所有的书籍信息
	public List<Commodity> getBooksList() {
	
		return dao.getBooksList();
	}
	/**
	 * 查询书籍的业务处理方法
	 * @return
	 */
	public Footet<Commodity> getFootet(int pagination,int record) {
		//定义Footet对象
		Footet<Commodity> footet=new Footet<Commodity>(pagination);
		//设置每页显示的纪录条数
		if(record>0) {
			footet.setRecord(record);
		}
		//获得当前条件下纪录的总条数
		footet.setTotalRecord(getBooksList().size());
		//验证当前页码是否正确，并赋值
		footet.setPagination(pagination);
		
		int startIndex=(footet.getPagination()-1)*footet.getRecord();
		
		//查询当前页显示的记录
		footet.setList(dao.getBooksFootet(startIndex, footet.getRecord()));
		return footet;
		
	}
	/**
	 * 查询指定书籍信息的业务层方法
	 * @param id
	 * @return
	 */
	public Commodity getBook(int id) {
		
		return dao.getBook(id);
	}
	/**
	 * 添加书籍到购物车的业务层方法
	 */
	public Boolean addBook(Indent indent) {
		int count=dao.addBook(indent);
		if(count!=0) {
			
			return true;
		}
		return false;
	}
	/**
	 * 查询订单的业务层方法
	 * @return
	 */
	public List<Indent> orderList(int id) {
		
		return dao.orderList(id);
	}
	
	
	public Indent queryShoppingCart(int userId, int bookId){
		
		return dao.queryShoppingCart(userId,bookId);
		
	}
	@Override
	public Boolean updateBook(Indent indent) {
		int a=dao.updateBook(indent);
		
		
		return a>0;
	}
	/**
	 * 删除指定id 的购物车
	 * @param id 购物车编号
	 * @return
	 */
	public Boolean delectBook(int id){
		
		int a=dao.delectBook(id);
		
		return a>0;
		
	}
	/**
	 * 新增订单
	 */
	public int addOrder(String[] id) {
		
		Connection conn=JDBCUtil.getConnection();
		int count=0;
		try {
			//改为手动提交
			conn.setAutoCommit(false);
			//新增订单
			int primary=dao.addOrder(id,conn);
			
			//新增订单详情
			int row=dao.addOrderInfo(id,primary,conn);
			
			//删除购物车
			dao.delectShoppingcartId(id,conn);
			conn.commit();
			count=primary;
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally {
			JDBCUtil.close(conn, null, null);
		}
		
		
		
		return count;
	}
	/**
	 * 修改购物车数量
	 */
	public Boolean updateShoppingCartNumber(String id, String number) {
		
		return dao.updateShoppingCartNumber(id,number)>0?true:false;
	}
	
}
